<?php

/*
  [Discuz!] (C)2001-2007 Comsenz Inc.
  This is NOT a freeware, use is subject to license terms

 */

function parse_template($file, $templateid, $tpldir)
{

    $nest = 5;
    $tpldir = $tpldir ? $tpldir : TPLDIR;
    //echo $tpldir;
    $tplfile = "$tpldir/$file.html";
    $objfile = ROOT_PATH . "/temp/{$file}_{$templateid}.tpl.php";

    if (!@$fp = fopen($tplfile, 'r')) {
        exit("Current template file '$tpldir/$file.html' not found or have no access!");
    }

    $template = fread($fp, filesize($tplfile));
    fclose($fp);

    $var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
    $const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";

    $template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
    $template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
    $template = preg_replace("/\{lang\s+(.+?)\}/ies", "languagevar('\\1')", $template);
    $template = preg_replace("/\{faq\s+(.+?)\}/ies", "faqvar('\\1')", $template);
    $template = str_replace("{LF}", "<?=\"\\n\"?>", $template);

    $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
    $template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
    $template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);

    $template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is", "\n<? include template('\\1'); ?>\n", $template);
    $template = preg_replace("/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is", "\n<? include template(\\1); ?>\n", $template);
    $template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<? \\1 ?>\n','')", $template);
    $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('<? echo \\1; ?>','')", $template);
    $template = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<? } elseif(\\1) { ?>\n','')", $template);
    $template = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n<? } else { ?>\n", $template);
    $template = preg_replace("/[\n\r\t]*\{cycle\s+\((.+?)\)\}[\n\r\t]*/ies", "stripvtags('<? echo cdb_cycle(\\1); ?>','')", $template);

    for ($i = 0; $i < $nest; $i++)
    {
        $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('\n<? if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\n\\3\n<? } } ?>\n')", $template);
        $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('\n<? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','\n\\4\n<? } } ?>\n')", $template);
        $template = preg_replace("/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies", "stripvtags('\n<? if(\\1) { ?>','\n\\2\n<? } ?>\n')", $template);
    }

    $template = preg_replace("/\{$const_regexp\}/s", "<?=\\1?>", $template);
    $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);

    $tempdir = dirname($objfile);
    if (!is_dir($tempdir) )@mkdir($tempdir);
    if (!@$fp = fopen($objfile, 'w')) {
        exit("Directory './templates/' not found or have no access!");
    }

    $template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "transamp('\\0')", $template);
    $template = preg_replace("/\<script[^\>]*?src=\"(.+?)\".*?\>\s*\<\/script\>/ise", "stripscriptamp('\\1')", $template);

    flock($fp, 2);
    fwrite($fp, $template);
    fclose($fp);
}

function transamp($str)
{
    $str = str_replace('&', '&amp;', $str);
    $str = str_replace('&amp;amp;', '&amp;', $str);
    $str = str_replace('\"', '"', $str);
    return $str;
}

function addquote($var)
{
    return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

function languagevar($var)
{
    if (isset($GLOBALS['language'][$var])) {
        return $GLOBALS['language'][$var];
    } else {
        return "!$var!";
    }
}

function stripvtags($expr, $statement)
{
    $expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
    $statement = str_replace("\\\"", "\"", $statement);
    return $expr . $statement;
}

function stripscriptamp($s)
{
    $s = str_replace('&amp;', '&', $s);
    return "<script src=\"$s\" type=\"text/javascript\"></script>";
}

function template($file, $templateid = 0, $tpldir = '')
{

    $tpldir = $tpldir ? $tpldir : TPLDIR;

    $tplfile = $tpldir . '/' . $file . '.html';
    $objfile = ROOT_PATH . '/temp/' . $file . '_' . $templateid . '.tpl.php';
    if ($templateid != 1 && !file_exists($tplfile)) {
        return template($file, 1, $tpldir);
    }

    if (@filemtime($tplfile) > @filemtime($objfile) || !file_exists($objfile)) {
        //require_once ROOT_PATH.'/common/template.func.php';
        parse_template($file, $templateid, $tpldir);
    }

    return $objfile;
}

function cdb_cycle()
{
    static $cycle = 0;
    $vars = func_get_args();
    if($cycle>=count($vars))
    {
        $cycle = 0;
        return $vars[$cycle];
    }
    else
    {
        $cycle++;
        $k = $cycle-1;
        return $vars[$k];
    }

}

?>